package com.sgcc.ln.pmos.e_ln_home.activity;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.arouter.home.HomeArouterPath;
import com.sgcc.ln.pmos.arouter.order.OrderArouterPath;
import com.sgcc.ln.pmos.common.base.MvpBaseActivity;
import com.sgcc.ln.pmos.common.bean.EventCenter;
import com.sgcc.ln.pmos.common.utils.ArguConstant;
import com.sgcc.ln.pmos.common.utils.DateUtils;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.ln.pmos.e_ln_home.bean.UserChageSaveBean;
import com.sgcc.ln.pmos.e_ln_home.bean.UserTypeChangeBean;
import com.sgcc.ln.pmos.e_ln_home.contract.UserTypeChangeContract;
import com.sgcc.ln.pmos.e_ln_home.presenter.UserTypeChangePresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 切换用户类型
 */
@Route(path = HomeArouterPath.HOME_USERTYPECHANGE)
public class UserTypeChangeActivity extends MvpBaseActivity<UserTypeChangePresenter> implements UserTypeChangeContract.View {

    private static final String TAG = "UserTypeChange";
    private TitleBar mTitleBar;
    CheckBox checkbox;
    TextView tv_time, tv_currentType, tv_selectType, tv_confirm, tv_checkcontent, tv_change_list;
    TimePickerView pvTime;
    LinearLayout liner_user_type;
    Calendar startDate = Calendar.getInstance();
    Calendar endDate = Calendar.getInstance();
    List<String> Selectlist = new ArrayList<>();
    List<UserTypeChangeBean.DataDTO.TargetListDTO> list = new ArrayList<>();
    OptionsPickerView pvOptions;
    private String selecttime, selectType, fileId;
    View.OnClickListener clickListener;

    @Autowired
    int year;

    @Autowired
    int month;
    @Autowired
    String yearAndMonth;

    @Override
    protected UserTypeChangePresenter createPresenter() {
        return new UserTypeChangePresenter();
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_usertypechange;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.setTitle("身份转换");
        mTitleBar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });

        checkbox = (CheckBox) findViewById(R.id.checkbox);
        tv_time = findViewById(R.id.tv_time);
        tv_currentType = findViewById(R.id.tv_currentType);
        tv_selectType = findViewById(R.id.tv_selectType);
        tv_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pvTime.show();
            }
        });

        liner_user_type = findViewById(R.id.liner_user_type);
        liner_user_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (pvOptions != null) {
                    pvOptions.show();
                }
            }
        });
        clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_checkcontent.setHighlightColor(getResources().getColor(android.R.color.transparent));
                if (checkbox.isChecked()) {
                    checkbox.setChecked(false);
                } else {
                    checkbox.setChecked(true);
                }
            }
        };
        tv_checkcontent = findViewById(R.id.tv_checkcontent);
        showCheckBox(false, null);
        tv_confirm = findViewById(R.id.tv_confirm);
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkbox.isChecked()) {
                    if (!TextUtils.isEmpty(selecttime) && !TextUtils.isEmpty(selectType)) {
                        mPresenter.ChangeUserType(selecttime, selectType);
                    } else {
                        ToastUtils.showShort("请选择完整");
                    }
                }
            }
        });
        tv_change_list = findViewById(R.id.tv_change_list);
        tv_change_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build(HomeArouterPath.HOME_USERTYPEHIS)
                        .navigation();
            }
        });
        initTimeSelect();
        initUserSelect();
        if (year != 0 && month != 0) {
            selecttime = DateUtils.getMonthBackString(1, "yyyy-MM");
            tv_time.setText(selecttime);
            tv_time.setClickable(false);
            mPresenter.getChangeContent(selecttime);
        }
    }

    private void showCheckBox(boolean shouldShow, String PdfName) {
        if (shouldShow) {
            checkbox.setVisibility(View.VISIBLE);
            tv_checkcontent.setVisibility(View.VISIBLE);

            SpanUtils.with(tv_checkcontent).append("我已阅读点击").setClickSpan(ColorUtils.getColor(R.color._333333), false, clickListener)
                    .append(PdfName).setClickSpan(ColorUtils.getColor(R.color._3274F0), false, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (!TextUtils.isEmpty(fileId)) {
                                ARouter.getInstance()
                                        .build(OrderArouterPath.ORDER_CONTRACTDETAILS)
                                        .withString("hyxqFileId", fileId)
                                        .navigation();
                            }
                        }
                    })
                    .append("，并完全理解和同意，自愿承担电力市场交易中的各种风险引致的全部后果及损失。").setClickSpan(ColorUtils.getColor(R.color._333333), false, clickListener)
                    .create();
        } else {
            checkbox.setVisibility(View.GONE);
            tv_checkcontent.setVisibility(View.GONE);
        }


    }

    private void initTimeSelect() {
        //正确设置方式 原因：注意事项有说明
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,1);
        // 获取当前年
        int year = calendar.get(Calendar.YEAR);
        // 获取当前月  原本应该+1  但pvTime处理了
        int month = calendar.get(Calendar.MONTH);
        startDate.set(year, month, 1);
        endDate.set(2099, 11, 1);
        //时间选择器
        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String time = TimeUtils.date2String(date, " yyyy-MM");
                selecttime = time.replace(" ", "");
                tv_time.setText(selecttime);
                mPresenter.getChangeContent(selecttime);
            }
        }).setType(new boolean[]{true, true, false, false, false, false})
                .setDecorView((ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content))
                .setRangDate(startDate, endDate)
                .build();
    }


    @Override
    public void queryChangeContentSuccess(UserTypeChangeBean bean) {
        if (bean != null) {
            if (bean.getStatus() == 0) {
                Selectlist.clear();
                if (bean.getData().getTargetList() != null) {
                    list = bean.getData().getTargetList();
                    for (int i = 0; i < bean.getData().getTargetList().size(); i++) {
                        Selectlist.add(bean.getData().getTargetList().get(i).getName());
                    }
                    if (pvOptions != null) {
                        pvOptions.setPicker(Selectlist);
                    }
                } else {
                    tv_selectType.setText("");
                    selectType = "";
                    fileId = "";
                }
                tv_currentType.setText(bean.getData().getOriginalText());
            } else {
                ToastUtils.showShort(bean.getMessage());
            }
        } else {
            ToastUtils.showShort("返回错误，身份转换信息获取失败");
        }
    }

    @Override
    public void queryChangeContentFaile() {
        ToastUtils.showShort("身份转换信息获取失败");
    }

    @Override
    public void changeUsrTypeSuccess(UserChageSaveBean bean) {
        if (bean != null) {
            ToastUtils.showShort(bean.getMessage());
            EventBus.getDefault().post(new EventCenter<>(ArguConstant.USER_TYPE_CHANGE));
            finish();
        } else {
            ToastUtils.showShort("返回错误，用户变更身份失败");
        }
    }

    @Override
    public void changeUsrTypeFaile() {
        ToastUtils.showShort("用户变更身份失败");
    }

    private void initUserSelect() {
        //条件选择器
        pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (list != null && list.size() != 0) {
                    tv_selectType.setText(Selectlist.get(options1));
                    selectType = list.get(options1).getValue();
                    fileId = list.get(options1).getFileInfoBo().get(0).getFileId();
                    showCheckBox(true, list.get(options1).getFileInfoBo().get(0).getFileName());
                }
            }
        }).setDecorView((ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content)).build();

    }


}
